<template>
	<view class="coupon-list">
		<view v-if="coupons.length === 0" class="empty">暂无优惠券</view>
		<view>
			<view v-for="item in coupons" :key="item.id" class="coupon-item"
				:class="{
					unused: item.status === 0 && isCouponAvailable(item),
					used: item.status === 1,
					expired: item.status === 2,
					frozen: item.status === 3,
					unavailable: !isCouponAvailable(item),
					selected: selectedId === item.id
				}"
				@click="selectCoupon(item)"
			>
				<view class="coupon-main-row">
					<view class="coupon-amount-block" :class="{ 'amount-unavailable': !isCouponAvailable(item) }">
						<view v-if="item.coupon && (item.coupon.coupon_type === 1 || item.coupon.coupon_type === 3)" class="amount-main">
							<text class="amount-symbol">￥</text><text class="amount-value">{{ item.coupon.amount }}</text>
						</view>
						<view v-else-if="item.coupon && item.coupon.coupon_type === 2" class="amount-main">
							<text class="amount-value">{{ item.coupon.discount }}</text><text class="amount-symbol">折</text>
						</view>
						<view class="coupon-type-tag">{{ couponTypeText(item.coupon && item.coupon.coupon_type) }}</view>
					</view>
					<view class="coupon-info-block">
						<view class="coupon-header-row">
							<text class="coupon-name">{{ item.coupon && item.coupon.coupon_name ? item.coupon.coupon_name : '优惠券' }}</text>
							<text class="coupon-status-badge" :class="[getStatusBadgeClass(item)]">{{ getCouponStatusText(item) }}</text>
						</view>
						<view class="coupon-min" v-if="item.coupon && item.coupon.min_amount">满{{ item.coupon.min_amount }}元可用</view>
						<view class="coupon-valid">{{ formatTime(item.coupon && item.coupon.end_time) }} 到期</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			coupons: [],
			loading: true,
			selectedId: null,
			source: null,
			orderAmount: 0
		}
	},
	onLoad({ source, amount}) {
    this.source = source
    this.orderAmount = amount ? parseFloat(amount) : 0
		this.fetchCoupons()
	},
	methods: {
		async fetchCoupons() {
			this.loading = true
			try {
				const res = await this.$api.getCouponList({
          user_id: this.userInfo.id,
          status: 0
        })
				this.coupons = res || []
			} catch (e) {
				this.coupons = []
			}
			this.loading = false
		},
		statusText(status) {
			switch (status) {
				case 0: return '未使用'
				case 1: return '已使用'
				case 2: return '已过期'
				case 3: return '已冻结'
				default: return '未知'
			}
		},
		couponTypeText(type) {
			switch(type) {
				case 1: return '满减券'
				case 2: return '折扣券'
				case 3: return '无门槛券'
				default: return ''
			}
		},
		formatTime(time) {
			if (!time) return ''
			const d = new Date(time)
			return `${d.getFullYear()}-${(d.getMonth()+1).toString().padStart(2,'0')}-${d.getDate().toString().padStart(2,'0')} ${d.getHours().toString().padStart(2,'0')}:${d.getMinutes().toString().padStart(2,'0')}`
		},
		selectCoupon(item) {
      // 检查优惠券是否可用
      if (!this.isCouponAvailable(item)) {
        uni.showToast({
          icon: 'none',
          title: '该优惠券不满足使用条件'
        })
        return
      }
      
			this.selectedId = item.id
			uni.setStorageSync('selectedCoupon', item)
			setTimeout(() => {
				uni.navigateBack()
			}, 150)
		},
		
		// 判断优惠券是否可用
		isCouponAvailable(coupon) {
      console.log(coupon.coupon.min_amount, this.orderAmount, this.source)
			// 首先检查优惠券状态
			if (coupon.status !== 0) {
				return false // 只有未使用的优惠券可用
			}
			
			// 如果是从支付页面来的，需要检查订单金额
			if (this.source === 'payment' && this.orderAmount > 0) {
				const couponInfo = coupon.coupon
				if (!couponInfo) return false
        console.log('couponInfo',couponInfo.min_amount)
				// 检查使用门槛
				if (couponInfo.min_amount && this.orderAmount < couponInfo.min_amount) {
					return false
				}
			}
			
			return true
		},
		
		// 获取优惠券可用状态文本
		getCouponStatusText(coupon) {
			// 如果是从支付页面来的，需要检查订单金额
			if (this.source === 'payment' && this.orderAmount > 0) {
				const couponInfo = coupon.coupon
				if (couponInfo && couponInfo.min_amount && this.orderAmount < couponInfo.min_amount) {
					return `满${couponInfo.min_amount}元可用`
				}
			}
			return this.statusText(coupon.status)
		},
		// 获取优惠券状态徽章样式类
		getStatusBadgeClass(coupon) {
			if (this.source === 'payment' && this.orderAmount > 0) {
				const couponInfo = coupon.coupon
				if (couponInfo && couponInfo.min_amount && this.orderAmount < couponInfo.min_amount) {
					return 'status-unavailable'
				}
			}
			return 'status-' + coupon.status
		}
	}
}
</script>

<style>
.coupon-list {
	padding: 32rpx 16rpx;
	background: #f6f7fb;
	min-height: 100vh;
}
.coupon-item {
	background: #fff;
	border-radius: 20rpx;
	margin-bottom: 32rpx;
	box-shadow: 0 4rpx 24rpx rgba(0,0,0,0.08);
	padding: 0 0 20rpx 0;
	display: flex;
	flex-direction: column;
	position: relative;
	overflow: hidden;
	transition: box-shadow 0.2s;
	border: 2rpx solid transparent;
}
.coupon-item.selected {
	border: 2rpx solid #4e8ef7;
	box-shadow: 0 6rpx 32rpx rgba(78,142,247,0.12);
}
.coupon-item.unused {
	border-left: 12rpx solid #4caf50;
}
.coupon-item.used {
	border-left: 12rpx solid #bdbdbd;
	opacity: 0.7;
}
.coupon-item.expired {
	border-left: 12rpx solid #f44336;
	opacity: 0.7;
}
.coupon-item.frozen {
	border-left: 12rpx solid #2196f3;
	opacity: 0.7;
}
.coupon-item.unavailable {
	border-left: 12rpx solid #888;
	opacity: 0.7;
}
.coupon-main-row {
	display: flex;
	flex-direction: row;
	align-items: stretch;
	padding: 32rpx 24rpx 12rpx 24rpx;
}
.coupon-amount-block {
	min-width: 160rpx;
	background: linear-gradient(135deg, #ff9800 60%, #ffd180 100%);
	border-radius: 16rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	margin-right: 24rpx;
	padding: 18rpx 12rpx 12rpx 0;
	position: relative;
}
.amount-main {
	display: flex;
	align-items: flex-end;
	justify-content: center;
}
.amount-symbol {
	font-size: 28rpx;
	color: #fff;
	font-weight: 400;
}
.amount-value {
	font-size: 54rpx;
	color: #fff;
	font-weight: bold;
	line-height: 1;
}
.coupon-type-tag {
	background: #fff3e0;
	color: #ff9800;
	font-size: 22rpx;
	border-radius: 8rpx;
	padding: 4rpx 16rpx;
	margin-top: 10rpx;
	font-weight: 500;
}
.coupon-info-block {
	flex: 1;
	display: flex;
	flex-direction: column;
	justify-content: center;
}
.coupon-header-row {
	display: flex;
	align-items: center;
	justify-content: space-between;
}
.coupon-name {
	font-weight: bold;
	color: #333;
	font-size: 32rpx;
	flex: 1;
	line-height: 1.2;
}
.coupon-status-badge {
	font-size: 22rpx;
	border-radius: 8rpx;
	padding: 4rpx 16rpx;
	margin-left: 16rpx;
	font-weight: 500;
	color: #fff;
}
.status-0 {
	background: #4caf50;
}
.status-1 {
	background: #bdbdbd;
}
.status-2 {
	background: #f44336;
}
.status-3 {
	background: #2196f3;
}
.status-unavailable {
	background: #888;
}
.coupon-min {
	font-size: 24rpx;
	color: #ff9800;
	margin: 8rpx 0 0 0;
}
.coupon-valid {
	font-size: 22rpx;
	color: #888;
	margin-top: 6rpx;
}
.coupon-footer {
	display: flex;
	flex-direction: row;
	justify-content: space-between;
	color: #bbb;
	font-size: 22rpx;
	padding: 0 24rpx;
}
.empty {
	text-align: center;
	color: #aaa;
	margin: 64rpx 0 0 0;
	font-size: 30rpx;
}
.amount-unavailable {
	background: linear-gradient(135deg, #888 60%, #ccc 100%) !important;
}
</style>
